<!-- templates/view_events.html -->
{% extends 'base.html' %}

{% block title %}查看 Webhook 事件 - DCO Webhook{% endblock %}

{% block content %}
    <h1>查看 Webhook 事件</h1>
    <form method="get" action="{{ url_for('view_events') }}" class="row g-3 mb-4">
        <div class="col-md-3">
            <input type="text" name="repository" class="form-control" placeholder="仓库名称" value="{{ filters.repository }}">
        </div>
        <div class="col-md-3">
            <input type="text" name="owner" class="form-control" placeholder="所有者" value="{{ filters.owner }}">
        </div>
        <div class="col-md-3">
            <input type="text" name="pusher" class="form-control" placeholder="Pusher" value="{{ filters.pusher }}">
        </div>
        <div class="col-md-3">
            <select name="invalid_commits" class="form-select">
                <option value="">所有提交</option>
                <option value="yes" {% if filters.invalid_commits == 'yes' %}selected{% endif %}>有无效提交</option>
                <option value="no" {% if filters.invalid_commits == 'no' %}selected{% endif %}>无无效提交</option>
            </select>
        </div>
        <div class="col-md-3">
            <input type="date" name="date_from" class="form-control" placeholder="开始日期" value="{{ filters.date_from }}">
        </div>
        <div class="col-md-3">
            <input type="date" name="date_to" class="form-control" placeholder="结束日期" value="{{ filters.date_to }}">
        </div>
        <div class="col-md-3">
            <button type="submit" class="btn btn-primary w-100">筛选</button>
        </div>
    </form>

    {% if events %}
        <div class="table-responsive">
            <table class="table table-bordered table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>仓库</th>
                        <th>所有者</th>
                        <th>Pusher</th>
                        <th>Ref</th>
                        <th>Before SHA</th>
                        <th>After SHA</th>
                        <th>接收时间</th>
                        <th>无效提交数</th>
                        <th>详情</th>
                    </tr>
                </thead>
                <tbody>
                    {% for event in events %}
                        <tr>
                            <td>{{ event.id }}</td>
                            <td>{{ event.repository_name }}</td>
                            <td>{{ event.repository_owner }}</td>
                            <td>{{ event.pusher }}</td>
                            <td>{{ event.ref }}</td>
                            <td><code>{{ event.before_sha[:7] }}</code></td>
                            <td><code>{{ event.after_sha[:7] }}</code></td>
                            <td>{{ event.received_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            <td>
                                {% if event.invalid_commits %}
                                    <span class="badge bg-danger">{{ event.invalid_commits|length }}</span>
                                {% else %}
                                    <span class="badge bg-success">0</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="#" class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#eventModal{{ event.id }}">查看</a>
                                
                                <!-- Modal -->
                                <div class="modal fade" id="eventModal{{ event.id }}" tabindex="-1" aria-labelledby="eventModalLabel{{ event.id }}" aria-hidden="true">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="eventModalLabel{{ event.id }}">事件详情 - ID {{ event.id }}</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="关闭"></button>
                                            </div>
                                            <div class="modal-body">
                                                <p><strong>仓库:</strong> {{ event.repository_owner }}/{{ event.repository_name }}</p>
                                                <p><strong>Pusher:</strong> {{ event.pusher }}</p>
                                                <p><strong>Ref:</strong> {{ event.ref }}</p>
                                                <p><strong>Before SHA:</strong> <code>{{ event.before_sha }}</code></p>
                                                <p><strong>After SHA:</strong> <code>{{ event.after_sha }}</code></p>
                                                <p><strong>接收时间:</strong> {{ event.received_at.strftime('%Y-%m-%d %H:%M:%S') }}</p>
                                                <hr>
                                                <h6>无效提交:</h6>
                                                {% if event.invalid_commits %}
                                                    <ul>
                                                        {% for commit in event.invalid_commits %}
                                                            <li>
                                                                <strong>SHA:</strong> <code>{{ commit.sha }}</code><br>
                                                                <strong>作者:</strong> {{ commit.author }}<br>
                                                                <strong>消息:</strong> {{ commit.message }}<br>
                                                                <strong>链接:</strong> <a href="{{ commit.url }}" target="_blank">{{ commit.url }}</a>
                                                            </li>
                                                        {% endfor %}
                                                    </ul>
                                                {% else %}
                                                    <p>所有提交均通过 DCO 验证。</p>
                                                {% endif %}
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    {% else %}
        <div class="alert alert-warning" role="alert">
            没有找到相关事件。
        </div>
    {% endif %}
{% endblock %}
